﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MegaBitProject.Models
{
    public class GameRepository : IGameRepository
    {
        private AppDataContext db = new AppDataContext();
        public IEnumerable<Application> getGames
        {
            get 
            {
                return db.Applications;
            }
            set
            {
            }
        }
        public IEnumerable<Game> getOpenGames
        {
            get 
            {
                return db.Games;
            }
            set
            { 
            }
        }
     
        public IEnumerable<PlayedGame> getPlayedGames
        {
            get
            {
                return db.PlayedGames;
            }
            set
            {
            }
        }

   
        public IEnumerable<Score> getScore
        {
            get
            {
                return db.Scores;
            }
            set
            {
            }
        }
        public IEnumerable<UserProfile> getUser
        {
            get
            {
                return db.UserProfiles;
            }
            set
            {
            }
        }

        public IEnumerable<Statistic> getStats
        {
            get
            {
                return db.Stats;
            }
            set
            {
            }
        }

        public IEnumerable<Pictionary> getPictionary
        {
            get
            {
                return db.Pictionaries;
            }
            set
            {
            }
        }


        public IEnumerable<WordList> getWord
        {
            get
            {
                return db.Words;
            }
            set
            {
            }
        }

        public IEnumerable<Complaints> getBans
        {
            get
            {
                return db.Ban;
            }
            set
            {
            }
        }

        public void newGame(Game g)
        {
            db.Games.Add(g);
            db.SaveChanges();

        }
        public void joinGame(PlayedGame g)
        {
            db.PlayedGames.Add(g);
            db.SaveChanges();
        }

        public void newPictionary(Pictionary p)
        {
            db.Pictionaries.Add(p);
            db.SaveChanges();
        }

        public void updatePictionary(int gameid, string newWord, string userName)
        {
            Pictionary result = new Pictionary();

            result = (from p in getPictionary
                          join u in getUser on p.userID equals u
                          join o in getOpenGames on p.gameID equals o
                          where p.gameID.ID == gameid
                          select p).SingleOrDefault();

            result.userID = (from u in getUser
                             where u.UserName.ToLower() == userName
                             select u).SingleOrDefault();

            result.word = newWord;

            db.SaveChanges();
        }

        public void updateScore(string id, int change)
        {
            var userID = id.ToLower();
            var result = (from score in getScore
                          join user in getUser
                          on score.userID equals user
                          where user.UserName.ToLower() == userID
                          select score).SingleOrDefault();

            if (result != null)
            {
                result.score = result.score + change;
            }
            else
            {
                Score s = new Score();
                s.gameID = (from game in getOpenGames
                            select game).FirstOrDefault();
                s.userID = (from user in getUser
                            where user.UserName.ToLower() == userID
                            select user).SingleOrDefault();
                s.score = change;
                db.Scores.Add(s);
            }
            db.SaveChanges();
        }

        public void updateStats(Statistic s)
        {
            db.Stats.Add(s);
            db.SaveChanges();
        }
        public void addBanList(Complaints c)
        {
            db.Ban.Add(c);
            db.SaveChanges();
        }
    }
}